package com.ywq.app.biz.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.ywq.app.biz.ArtPicBiz;
import com.ywq.app.biz.ArtRoomBiz;
import com.ywq.app.biz.IdentifierBiz;
import com.ywq.app.dao.ArtPicDao;
import com.ywq.app.domain.ArtArtistPic;
import com.ywq.app.domain.ArtAuctionPic;
import com.ywq.app.domain.ArtClass;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.domain.ArtRoomPic;
import com.ywq.app.domain.PicBidLog;
import com.ywq.common.constant.Constant;
import com.ywq.common.constant.PageSize;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
import com.ywq.common.util.Common;
import com.ywq.common.util.IdGenerator;



public class ArtPicBizImpl implements  ArtPicBiz {

	private final static Logger logger = LoggerFactory
			.getLogger(ArtPicBizImpl.class);
	
	private ArtPicDao artPicDao;
	@Autowired
	private ArtRoomBiz artRoomBiz;
	@Autowired
	private IdentifierBiz idBiz;
	public void setArtPicDao(ArtPicDao artPicDao) {
		this.artPicDao = artPicDao;
	}
	//作品集合   频道类 型
	@Override
	public PageModel getArtPicList(Integer page,String picType) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_ADMIN);
		map.put("pageSize", PageSize.PAGE_SIZE_ADMIN);
		map.put("type", picType);
		int count = artPicDao.getArtPicListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artPicDao.getArtPicList(map));
		pm.setPageSize(PageSize.PAGE_SIZE_ADMIN);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_ADMIN - 1) / PageSize.PAGE_SIZE_ADMIN);
		return pm;
	}
	
	@Override
	public void insertArtPic(ArtPic artPic) throws AppBusinessException {
		//获取id
		String id = idBiz.generateId("art_pic");
		artPic.setIdentifier(id);
		String type =artPic.getType();
		String classId =artPic.getClassId();
		if(!"".equals(classId)){
			if(!"".equals(type)){
				if("room".equals(type)){
					//展览馆：增加展览馆与作品关联表
					ArtRoomPic artRoomPic =new ArtRoomPic();
					artRoomPic.setPicId(id);
					artRoomPic.setRoomId(classId);
					artPicDao.insertArtPic(artPic);
					artRoomBiz.insertArtRoomPic(artRoomPic);
				}else if("made".equals(type)){
					//定制(艺术家与画关联)
					ArtArtistPic artArtistPic =new ArtArtistPic();
					artArtistPic.setPicId(id);
					artArtistPic.setArtistId(classId);
					artPicDao.insertArtPic(artPic);
					artPicDao.insertArtArtistPic(artArtistPic);
				}else if("auction".equals(type)){
					//拍卖（作品关联表）
					ArtAuctionPic artAuctionPic =new ArtAuctionPic();
					artAuctionPic.setAuctionId(classId);
					artAuctionPic.setPicId(id);
					artPicDao.insertArtPic(artPic);
					artPicDao.insertArtAuctionPic(artAuctionPic);
				}else if("find".equals(type)){
					//发现（作品关联表）
					ArtArtistPic artArtistPic =new ArtArtistPic();
					artArtistPic.setPicId(id);
					artArtistPic.setArtistId(classId);
					artPicDao.insertArtPic(artPic);
					artPicDao.insertArtArtistPic(artArtistPic);
				}
			}
		}
		
	}
	/**
	 * 作品底层页面
	 */
	@Override
	public ArtPic getArtPic(String picId) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", picId);
		ArtPic artPic =artPicDao.getArtPic(map);
		return artPic;
	}
	//作品分类得到子类
	/**
     * author BruceZhang
     * @param map   parent 父ID
     * @return  根据父ID取得子节点的树结构
     * @throws AppBusinessException
     */
    @Override
    public List<ArtClass> getChildById(String parent,String type) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parent", parent);
        map.put("type", type);
        return artPicDao.getChildById(map);
    }
    //添加分类
    @Override
	public void addArtClass(ArtClass artClass) throws AppBusinessException {
    	artClass.setIdentifier(IdGenerator.createIdentifier());
		if("0".equals(artClass.getParent())){
			artClass.setDeep(1);
		}else{
			artClass.setDeep(getClassById(artClass.getParent()).getDeep()+1);
		}
		artPicDao.addClass(artClass);
	}
  //根据频道Id取得分类信息
	@Override
	public ArtClass getClassById(String classId) throws AppBusinessException {
		return artPicDao.getClassById(classId);
	}
	//更新分类
    @Override
    public void updateClassById(ArtClass artClass) throws AppBusinessException {
    	artPicDao.updateClassById(artClass);
    }
    //删除作品
    @Override
    public void deletePic(String identifier) throws AppBusinessException {
    	artPicDao.deletePic(identifier);
    	artPicDao.deletePicArtist(identifier);
    }
    //更新作品
    @Override
    public void updatePic(ArtPic artPic) throws AppBusinessException {
    	artPicDao.updatePic(artPic);
    }
  //更新浏览数
    @Override
    public void updatePicViewAmount(ArtPic artPic) throws AppBusinessException {
    	artPicDao.updatePicViewAmount(artPic);
    }
    
    @Override
	public PageModel getArtPicListForMisc(Integer page,String picType,Integer pageSize) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("type", picType);
		int count = artPicDao.getArtPicListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artPicDao.getArtPicList(map));
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}
    //查看拍卖最高价纪录
    @Override
    public PicBidLog getPicBidLogByPicId(String picId,String auctionId) throws AppBusinessException {
    	if(!Common.emptyStr(picId)){
	    	Map<String, Object> map = new HashMap<String, Object>();
	    	map.put("picId", picId);
	    	map.put("auctionId", auctionId);
			return artPicDao.getPicBidLogByPicId(map);
    	}
    	return null;
	}
    //添加出价纪录
    @Override
    public void insertPicBidLog(PicBidLog newBid) throws AppBusinessException {
    	String id = idBiz.generateId("pic_bid_log");
    	newBid.setId(id);
    	artPicDao.insertPicBidLog(newBid);
    }
    //作品不可买卖
    @Override
    public void noForwardPic(String picId) throws AppBusinessException {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("picId", picId);
    	map.put("isForward", true);
    	map.put("type", "auction");
    	artPicDao.noForwardPic(map);
    }
    //查看同类作品 4个
    @Override
    public List<ArtPic> getSameClassPicList(String classId) throws AppBusinessException {
    	if(!Common.emptyStr(classId)){
	    	Map<String, Object> map = new HashMap<String, Object>();
	    	map.put("startId", 0);
			map.put("pageSize", 4);
			map.put("type", "find");
			map.put("artClass", classId);
			return artPicDao.getArtPicList(map);
    	}
    	return null;
	}
    //查询所有的分类
    @Override
	public PageModel getAllClassPageModel(Integer page, Integer pageSize,String name) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("name", name);
		int count = artPicDao.getAllClassCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artPicDao.getAllClass(map));
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}
}
